﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace CWCS_OL.print
{
    public partial class PrintTemplate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.GetCurrentUser().hasPermission("ptr"))
            {
                Response.Write("没有权限查看打印模板列表");
                Response.End();
            }

            if(!this.GetCurrentUser().hasPermission("ptu"))
            {
                Panel_add.Visible = false;
                SqlDataSource_template.UpdateCommand = "";
                CommandField cf = (CommandField)GridView1.Columns[0];
                cf.ShowEditButton = false;
            }

            if(!this.GetCurrentUser().hasPermission("ptd"))
            {
                GridView1.AutoGenerateDeleteButton = false;
                SqlDataSource_template.DeleteCommand = "";
                CommandField cf = (CommandField)GridView1.Columns[0];
                cf.ShowDeleteButton = false;
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            if (!this.GetCurrentUser().hasPermission("ptd"))
                return;

            using (DataBase db = new DataBase())
            {
                db.AddParameter("tid", e.Keys["id"]);
                db.ExecuteNonQuery("DELETE FROM [PrintTemplate] WHERE [tid] = @tid");
            }
        }

        protected void Button_add_Click(object sender, EventArgs e)
        {
            if (!this.GetCurrentUser().hasPermission("ptu"))
                return;

            using (DataBase db = new DataBase())
            {
                db.AddParameter("type", DropDownList_type.SelectedValue);
                db.AddParameter("name", "新打印模板");
                DataTable dt = db.ExecuteDataTable("INSERT INTO [PrintConfig] ([type], [name], [deftemplate]) VALUES (@type, @name, 0) SELECT SCOPE_IDENTITY()");
                if (dt.Rows.Count == 1)
                {
                    Response.Redirect("PrintEdit.aspx?id=" + dt.Rows[0][0].ToString());
                }
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            if (this.GetCurrentUser().hasPermission("ptu"))
            {
                Response.Redirect("PrintEdit.aspx?id=" + GridView1.DataKeys[e.NewEditIndex]["id"].ToString());
            }
        }

    }
}
